package cn.ffcs.product.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.echoice.paper.PageBean;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.product.model.Product;
import cn.ffcs.productdict.model.ProductInfo;

/**
 * 
 * 字典类型DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author g.huangjx
 * @version $Id$
 */
public class ProductDAO<D extends Product> extends BaseDAO<D> implements IProductDAO<D>
{
    /**
     * 获取字典类型实体类
     * @param b
     * @return
     */
    public String getEntityName()
    {
        return new Product().getClass().getName();
    }
    /**
     * 延迟加载产品动态属性
     * @param b
     * @return
     */
    public D findById(Integer id)
    { 
        D d=(D) getHibernateTemplate().get(getEntityName(), id);
        Hibernate.initialize(d.getProductInfos());
        Hibernate.initialize(d.getProductManagers());
        return d;
       
    }
    /**
     * 删除产品所有属性
     * @param b
     * @return
     */
    public void deleteProductInfo(String productId){
        getSession().createQuery("delete from ProductInfo where product="+productId).executeUpdate();
    }
    /**
     *循环保存产品属性
     * @param b
     * @return
     */
    public void saveProductInfo(D d,String addInfos,String addValues){
        String ids[]=addInfos.split(",");
        String values[]=addValues.split(",");
        for(int i=0;i<ids.length;i++){
            ProductInfo productInfo=new ProductInfo();
            productInfo.setPRODUCT(d);
            productInfo.setProductProperty(ids[i]);
            productInfo.setBegin(getCurrSysdate());
            productInfo.setPropertyValue(values[i]);
            Long l=Long.valueOf("9783941568000");
            Date end=new Date(l);
            productInfo.setEnd(end);
            getHibernateTemplate().save(productInfo);
        }
    }
    
    public void saveOneProductInfo(D d,String addInfos,String addValues){
    	ProductInfo productInfo=new ProductInfo();
        productInfo.setPRODUCT(d);
        productInfo.setProductProperty(addInfos);
        productInfo.setBegin(getCurrSysdate());
        productInfo.setPropertyValue(addValues);
        Long l=Long.valueOf("9783941568000");
        Date end=new Date(l);
        productInfo.setEnd(end);
        getHibernateTemplate().save(productInfo);
    }
    /**
     * 删除产品所有负责人
     * @param b
     * @return
     */
    
    public void deletePerson(String productId){
        getSession().createQuery("delete from ProductManager where product="+productId).executeUpdate();
    }
    /**
     * 根据产品目录查产品
     * @param p
     * @return
     */
    public List<Product> findBySort(String sortId){
        if(sortId==null||sortId.equals("0")){
            return null;
        }
        String hsq="from Product a  where a.id in(select b.product from ProductRefSort b where b.sort="+sortId+")";
        return super.getSession().createQuery(hsq).list();
    }
    
    /**
     * 删除产品所有负责人
     * @param b
     * @return
     */
    public void deleteProductRefSort(Integer productId){
        getSession().createQuery("delete from ProductRefSort  a where a.product="+productId).executeUpdate();
    }
    
    /**
     * 按条件查询翻页
     * @param cond
     * @param currPage
     * @param pageSize
     * @return
     */
    @SuppressWarnings("unchecked")
    public PageControl findByPage(HashMap<String, Object> conds,int currPage, int pageSize)
    {
      String hql="select a from Product a  where (exists(select t from ProductManager t where t.product=a.id and t.person='"+conds.get("person")+"') or a.creator='"+conds.get("creator")+"')";
      if(conds.containsKey("name")&&!conds.containsValue("")){
          hql+=" and a.name like '%"+conds.get("name")+"%'";
      }
      
//        String hsq="select a from (select distinct t.product,t.person from ProductManager t) b,Product a  where b.product=a.id and (a.creator='"+conds.get("creator")+"' or b.person='"+conds.get("person")+"')";
//        return super.getSession().createQuery(hsq).list();
        List results=super.getSession().createQuery(hql).list();
        PageControl pageControl=new PageControl(results.size(),currPage,pageSize);
        int startRow = pageControl.getStart();
        int endRow=pageControl.getPageSize();
        return new PageControl(results, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
    }
    
    
    
}
